const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatDate = date => {
  const dt = new Date(date)
  const year = dt.getFullYear()
  const month = dt.getMonth() + 1
  const day = dt.getDate()

  return [year, month, day].map(formatNumber).join('-')
}

const timestampToDate = timestamp => {
  let date = new Date(timestamp);
  let year = date.getFullYear();
  let month = date.getMonth() + 1;
  let day = date.getDate();
  month = month < 10 ? "0" + month : month;
  day = day < 10 ? "0" + day : day;
  return year + '-' + month + '-' + day;
}

const timestampToTime = timestamp => {
  let date = new Date(timestamp);
  let year = date.getFullYear();
  let month = date.getMonth() + 1;
  let day = date.getDate();
  month = month < 10 ? "0" + month : month;
  day = day < 10 ? "0" + day : day;
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  month = month < 10 ? "0" + month : month;
  day = day < 10 ? "0" + day : day;
  return year + '-' + month + '-' + day+ ' ' + [hour, minute, second].map(formatNumber).join(':');
  
}
const dateToTimestamp = date=>{
  var timestamp = Date.parse(date);
  return timestamp
}

const timestampToLocalFormat = timestamp => {
  let date = new Date(timestamp);
  let month = date.getMonth() + 1;
  let day = date.getDate();
  return month + "月" + day + "日";
}

const getRegisterNoNumber = (schelDate, am, pm, eve, amNum, pmNum, eveNum, amState, pmState, eveState) => {
  const date = new Date(schelDate);
  const dateAm = Date.parse(formatDate(date) + ' ' + am)
  if (amState == "Y") {
    if (Date.parse(date) <= dateAm) {
      return {
        state: '1',
        number: amNum
      }
    } else {
      return {
        state: '1',
        number: 0
      }
    }
  }
  if (pmState == "Y") {
    const datePm = Date.parse(formatDate(date) + ' ' + pm)
    if (Date.parse(date) <= datePm) {
      return {
        state: '2',
        number: pmNum
      }
    } else {
      return {
        state: '2',
        number: 0
      }
    }
  }
  const now = new Date(schelDate)
  const dateEve = Date.parse(formatDate(now.setDate(now.getDate() + 1)) + ' ' + eve)
  if (eveState == "Y") {
    if (Date.parse(date) <= dateEve) {
      return {
        state: '3',
        number: eveNum
      }
    } else {
      return {
        state: '3',
        number: 0
      }
    }
  }
  return 0
}
/**
 * 过滤排班
 * @param {排班时间} schelDate 
 * @param {上午号结束时间} am 
 * @param {下午号结束时间} pm 
 * @param {晚上号结束时间} eve 
 * @param {上午号启用状态} amState 
 * @param {下午好启用状态} pmState 
 * @param {晚上号启用状态} eveState 
 */
const filterScheduling = (schelDate, am, pm, eve, amState, pmState, eveState) => {
  const date = new Date();
  if (timestampToDate(schelDate) != formatDate(date)) {
    return true;
  }
  const dateAm = Date.parse(formatDate(date) + ' ' + am);
  if (Date.parse(date) <= dateAm) {
    if (amState == "Y") {
      return true;
    } else {
      return false;
    }
  }
  const datePm = Date.parse(formatDate(date) + ' ' + pm);
  if (Date.parse(date) <= datePm) {
    if (pmState == "Y") {
      return true;
    } else {
      return false;
    }
  }

  const now = new Date();
  const dateEve = Date.parse(formatDate(now.setDate(now.getDate() + 1)) + ' ' + eve);
  if (Date.parse(date) <= dateEve) {
    if (eveState == "Y") {
      return true;
    } else {
      return false;
    }
  }

  return false;
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

module.exports = {
  formatTime: formatTime,
  formatDate: formatDate,
  timestampToDate: timestampToDate,
  timestampToLocalFormat: timestampToLocalFormat,
  getRegisterNoNumber: getRegisterNoNumber,
  filterScheduling: filterScheduling,
  timestampToTime: timestampToTime,
  dateToTimestamp: dateToTimestamp
}